import docx2txt

from langchain_community.document_loaders import Docx2txtLoader
from langchain_community.document_loaders.base import BaseLoader
from abc import ABC

# 导入类型检查库
from typing import List, Union
from pathlib import Path
# 导入继承库
from langchain_community.document_loaders.base import BaseLoader
from abc import ABC
# 导入处理docx文件的库
from docx import Document

class DocxReader(BaseLoader, ABC):
    """
    Load "Doc" file
    """
    def __init__(self, file_path:Union[str, Path]):
        """Initialize with file path"""
        self.file_path = file_path
        self.page_content = Document(self.file_path)

    def __del__(self):
        pass

    def load(self):
        """Get file data"""
        paragraphs_list = self.read_paragraphs()
        p_texts = self.concat_text(paragraphs_list)
        return p_texts
    
    def read_paragraphs(self):
        """读取文件中所有内容"""
        paragraphs = []
        for paragraph in self.page_content.paragraphs:
            if len(paragraph.text) <= 0:
                continue
            paragraphs.append(paragraph.text)
        return paragraphs
    
    def read_tables(self):
        """读取所有表格内容"""
        tables_data = []
        for table in self.page_content.tables:
            rows_data = []
            for row in table.rows:
                cells_data = [cell.text for cell in row.cells]
                rows_data.append(cells_data)
            tables_data.append(rows_data)
        return tables_data
    
    def concat_text(self, text_list):
        assert type(text_list) == list, "data type is not list"

        all_text = "".join(text_list)
        return all_text

    # def readAll_to_text(self):
    #     output = subprocess.check_output(['antiword', self.file_path])
    #     return output.decode('utf-8')
